package com.laicunba.ext.activity.act20170410;

import java.util.Date;

import org.springframework.stereotype.Service;

import com.laicunba.pojo.ActivityDO;
import com.laicunba.pojo.UserDO;
import com.laicunba.service.activity.ActivityChanceService;
import com.laicunba.util.CacheUtil;
import com.laicunba.util.DateUtil;
import com.laicunba.util.StringUtil;

//@Service
public class Act20170410ChanceService implements ActivityChanceService {

	private static int HOUR24 = 3600 * 24;

	@Override
	public String getActivityId() {
		return "20170410";
	}

	@Override
	public boolean hasChance(UserDO userDO, ActivityDO activityDO, String subActId) {
		if ("CJ".equalsIgnoreCase(subActId)) {
			String cjCountKey = "act.20170410CJ." + userDO.getId() + DateUtil.formatDate(new Date());
			String chanceKey = "act.20170410chance." + userDO.getId() + DateUtil.formatDate(new Date());
			int todayTotalChance = StringUtil.isNotEmpty(CacheUtil.getAttribute(chanceKey)) ? Integer.parseInt(CacheUtil.getAttribute(chanceKey)) : 0 ;
			int todayCjCount = StringUtil.isNotEmpty(CacheUtil.getAttribute(cjCountKey)) ? Integer.parseInt(CacheUtil.getAttribute(cjCountKey)) : 0 ;
			if (todayCjCount > 5 || todayTotalChance - todayCjCount <= 0) {
				return false;
			} else {
				CacheUtil.incr(cjCountKey, HOUR24);
				return true;
			}
		}

		return false;
	}

}
